package client;

import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

import bean.Pad;
import bean.Phone;
import bean.Watch;
import abstractFactory.Factory;
import abstractFactory.KindOfFactory;
import java.awt.BorderLayout;

public class AbstractFactoryMethodClient {
	

	static Factory mifactory;
	static Factory appleFactory;
	static Factory samsungFactory;
	
	static JLabel message;
	@SuppressWarnings("unchecked")
	public static void main(String[] args) {
		JFrame mainFrame = new JFrame("AbstractFactoryMethodClient");
		mainFrame.setBounds(100, 100, 400, 300);
		
		JPanel mainPanel = new JPanel();
		mainPanel.setBounds(0, 0, 400, 300);
		//build factory
		mifactory = Factory.buildFactory(KindOfFactory.MiFactory);
		appleFactory = Factory.buildFactory(KindOfFactory.AppleFactory);
		samsungFactory = Factory.buildFactory(KindOfFactory.SamsungFactory);
		
		//comboBox
		@SuppressWarnings("rawtypes")
		final JComboBox factoryBox = new JComboBox();
		@SuppressWarnings("rawtypes")
		final JComboBox productBox = new JComboBox();
		JPanel selectedPanel = new JPanel();
		
		factoryBox.addItem("小米公司");
		factoryBox.addItem("三星公司");
		factoryBox.addItem("苹果公司");
		
		productBox.addItem("手机");
		productBox.addItem("平板");
		productBox.addItem("手表");

		
		JLabel factoryLab = new JLabel("请选择一个公司");
		JLabel productLab = new JLabel("请选择一种产品");
		JButton getBtn = new JButton("买买买");
		selectedPanel.setLayout(new FlowLayout());
		selectedPanel.add(factoryLab);
		selectedPanel.add(factoryBox);
		selectedPanel.add(productLab);
		selectedPanel.add(productBox);
		selectedPanel.add(getBtn);
		message = new JLabel();
		
		//mainPanel.add(getBtn);
		getBtn.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent arg0) {
				int prodectIndex = productBox.getSelectedIndex();
				
				KindOfFactory factoryEnumber = KindOfFactory.MiFactory;
				switch(factoryBox.getSelectedIndex())
				{
				case 0:factoryEnumber = KindOfFactory.MiFactory;break;
				case 1:factoryEnumber = KindOfFactory.SamsungFactory;break;
				case 2:factoryEnumber = KindOfFactory.AppleFactory;break;
				}
				switch(prodectIndex)
				{
				case 0:
					Phone phone = null;
					switch(factoryEnumber)
					{
					case AppleFactory:phone = appleFactory.getPhone();break;
					case MiFactory:phone = mifactory.getPhone();break;
					case SamsungFactory:phone = samsungFactory.getPhone();break;
					}
					message.setText(phone.getMessage());
					break;
				case 1:
					Pad pad = null;
					switch(factoryEnumber)
					{
					case AppleFactory: pad = appleFactory.getPad();break;
					case MiFactory:pad = mifactory.getPad();break;
					case SamsungFactory:pad = samsungFactory.getPad();break;
					}
					message.setText(pad.getBrand());
					break;
				case 2:
					Watch watch = null;
					switch(factoryEnumber)
					{
					case SamsungFactory:watch = samsungFactory.getWatch();break;
					case MiFactory:watch = mifactory.getWatch();break;
					case AppleFactory:watch = appleFactory.getWatch();break;
					}
					message.setText(watch.getBrand());
					break;
				}
			}
		});
		
		
		//label
		JPanel labelPanel = new JPanel();
		labelPanel.setLayout(new FlowLayout());
		message.setText("Please select");
		labelPanel.add(message);
		mainPanel.setLayout(new BorderLayout(0, 0));
		mainPanel.add(selectedPanel, BorderLayout.CENTER);
		mainPanel.add(labelPanel, BorderLayout.SOUTH);
		
		
		mainFrame.getContentPane().add(mainPanel);
		mainFrame.setVisible(true);
		
	}
}
